﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.StandardUI;
using QLBH.Model.Controller;
using QLBH.ModelInterface;
using QLBH.DataModel;
using QLBH.Base;

namespace QLBH.SaleProduct
{
    public partial class CalcGiaBanDlg : Form
    {
        private List<IQLMatHangModel> models;
        public CalcGiaBanDlg()
        {
            InitializeComponent();

            models= new List<IQLMatHangModel>();
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }               
        private void CalcGiaBanDlg_Load(object sender, EventArgs e)
        {
            dataGridViewEx1.InitCheckboxColumnHeader();
            // Load DS Nganh hang  va loai hang

            // Load DS Ngang Hang

            List<NGANHHANG> nganhhangModels = NganhHangController.GetInstance().GetAll().ToList();

            List<LOAIHANG> loaihangModels = NhomHangController.GetInstance().GetAll().ToList();

            foreach (NGANHHANG nh in nganhhangModels)
            {
                TreeNode treeNode = new TreeNode();
                treeNode.Tag = nh.ID;
                treeNode.Text = nh.TENNH;
                treeView1.Nodes.Add(treeNode);

                List<LOAIHANG> lhs = loaihangModels.Where(lh => lh.NGANHHANGID == nh.ID).ToList();

                foreach (LOAIHANG lh in lhs)
                {
                    TreeNode nhnode = new TreeNode();
                    nhnode.Tag = lh.LOAIHANGID;
                    nhnode.Text = lh.TENLOAIHANG;
                    treeNode.Nodes.Add(nhnode);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void CalcGiaBanDlg_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode==Keys.S)
            {
                btnUpdate_Click(btnUpdate, null);
            }
            else if (e.KeyCode==Keys.Escape)
            {
                btnClose_Click(btnClose, null);
            }
            else if (e.Control && e.KeyCode==Keys.F)
            {
                OnFindMH();
            }            
        }
        private void OnFindMH()
        {
            FrmFind frmFindDlg=new FrmFind();
            if (frmFindDlg.ShowDialog()==DialogResult.OK)
            {

            }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode treenode = e.Node as TreeNode;            

            GetMatHangFromSelected(treenode);            

        }
        private void GetMatHangFromSelected(TreeNode treeNode)
        {
            models.Clear();
            dataGridViewEx1.DataSource = models;
            if (treeNode.Parent == null)
            {
                //nganh hang selected
                Guid nganhhangid = (Guid)treeNode.Tag;

                List<LOAIHANG> loaihangs = NhomHangController.GetInstance().FindBy(
                    nh => nh.NGANHHANGID==nganhhangid).ToList();

                List<MATHANG> mathangs = new List<MATHANG>();
                foreach (LOAIHANG lh in loaihangs)
                {
                    mathangs.AddRange(MatHangController.GetInstance().FindBy(mh => 
                        mh.NHOMHANGID == lh.LOAIHANGID).ToArray());
                }

                foreach (MATHANG mh in mathangs)
                {
                    IQLMatHangModel model = ModelFactory.ModelFactory.GetQLMatHangModelInstance();
                    model.InitFromEntity(mh);

                    models.Add(model);
                }

                dataGridViewEx1.DataSource = models.ToArray();                
            }
            else
            {
                // loai hang selected
                Guid nhomhangid = (Guid)treeNode.Tag;

                List<MATHANG> mathangs = new List<MATHANG>();

                mathangs = MatHangController.GetInstance().FindBy(mh =>
                    mh.NHOMHANGID == nhomhangid).ToList();                                

                foreach (MATHANG mh in mathangs)
                {
                    IQLMatHangModel model = ModelFactory.ModelFactory.GetQLMatHangModelInstance();
                    model.InitFromEntity(mh);

                    models.Add(model);
                }

                dataGridViewEx1.DataSource = models.ToArray();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                //Check value input is correct format
                if (!CheckValiddata())
                {
                    return;
                }

                //Update value for list mathang
                for (int i = 0; i < dataGridViewEx1.RowCount; i++)
                {
                    if (dataGridViewEx1[0, i].Value != null && (bool)dataGridViewEx1[0, i].Value)
                    {
                        //row selected

                        if (rbtnTiLe.Checked)
                        {
                            //ti le
                            decimal delTileBL = nrPercentBL.Value;
                            decimal deltileBB = nPercentBB.Value;
                            models[i].GIABANLE = models[i].GIANHAP * (1 + delTileBL / 100);
                            models[i].GIABANBUON = models[i].GIANHAP * (1 + deltileBB / 100);
                        }
                        else
                        {
                            //Increase money
                            decimal delTileBL = decimal.Parse(tbxIncreaseMoneyBL.Text);
                            decimal deltileBB = decimal.Parse(tbxIncreaseMoneyBB.Text);
                            models[i].GIABANLE = models[i].GIANHAP + delTileBL;
                            models[i].GIABANBUON = models[i].GIANHAP + deltileBB;
                        }
                        //Save to database
                        MatHangController.GetInstance().Edit(models[i].ToEntity());
                    }

                }
                dataGridViewEx1.DataSource = models;                
                //(dataGridViewEx1.BindingContext[dataGridViewEx1.DataSource] as CurrencyManager).Refresh();               
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private bool CheckValiddata()
        {
            try
            {
                DataInputHelper.CheckDecimalData(tbxIncreaseMoneyBL.Text, "Giá tăng bán lẻ");
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxIncreaseMoneyBL.SelectAll();
                tbxIncreaseMoneyBL.Focus();

                return false;
            }
            try
            {
                DataInputHelper.CheckDecimalData(tbxIncreaseMoneyBB.Text, "Giá tăng bán buôn");
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
                tbxIncreaseMoneyBB.SelectAll();
                tbxIncreaseMoneyBB.Focus();

                return false;
            }
            return true;
        }

        private void rbtnTiLe_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnTiLe.Checked)
            {
                nrPercentBL.Enabled = true;
                tbxIncreaseMoneyBL.Enabled = false;
            }
            else
            {
                nrPercentBL.Enabled = false;
                tbxIncreaseMoneyBL.Enabled = true;
            }
        }
    }
}
